﻿#include "Gui.h"

#include "App/Window.h"

static Gui *s_gui = nullptr;

Gui::Gui(const Ref<Window> &window)
    : m_window(window)
{
  assert(window && "Window is null");
  assert(!s_gui && "Gui already exists");
  s_gui = this;
  Init();
}

Gui::~Gui()
{
  ImGui_ImplOpenGL3_Shutdown();
  ImGui_ImplGlfw_Shutdown();
  ImGui::DestroyContext();
}

void Gui::Init()
{
  ImGui::CreateContext();
  ImGui::StyleColorsDark();
  ImGui_ImplGlfw_InitForOpenGL(m_window->GetNativeWindow(), true);
  ImGui_ImplOpenGL3_Init("#version 450 core");
}

Gui &Gui::GetInstance()
{
  return *s_gui;
}

void Gui::BeginFrame()
{
  ImGui_ImplOpenGL3_NewFrame();
  ImGui_ImplGlfw_NewFrame();
  ImGui::NewFrame();
}

void Gui::FinalizeFrame()
{
  ImGui::Render();
  ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

ImGuiContext *Gui::GetContext() const
{
  return ImGui::GetCurrentContext();
}
